<?php
include("include/conn.php");
  require('include/Smarty/libs/Smarty.class.php');

  $msg     = '';
  if(isset($_POST['submit']))
  {
    $partyId             = isset($_REQUEST['partyIdEdit']) ? $_REQUEST['partyIdEdit'] : 0;
	  $bullionPartyName    = isset($_REQUEST['bullionPartyName']) ? $_REQUEST['bullionPartyName'] : 0;
	  $bullionAddress      = ($_REQUEST['bullionAddress']) ? $_REQUEST['bullionAddress'] : 0;
	  $partyMobile         = ($_REQUEST['partyMobile']) ? $_REQUEST['partyMobile'] : 0;
	  $bullionFromDate     = isset($_REQUEST['toDateYear']) ? $_REQUEST['toDateYear']."-".$_REQUEST['toDateMonth']."-".$_REQUEST['toDateDay'] : 0;
	  $updateQueryProperty = "UPDATE bullionparty 
                               SET bullionPartyName  = '".$bullionPartyName."',
                                     bullionAddress  = '".$bullionAddress."',
                                        partyMobile  = '".$partyMobile."',
                                    bullionFromDate  = '".$bullionFromDate ."'
                             WHERE          partyId  = '".$partyId ."'";
	  $updateQueryPropertyRequest = mysql_query($updateQueryProperty);
	  if(!$updateQueryPropertyRequest)
	  {
	    die("Update Query Not Inserted : ".mysql_error());
	  }
	  else
	  {
	 		header("Location:bullionPartyList.php");
	  }
	}
	// Selection OF Bullion Prev Fine ANd Amount :Start
  $total['kg']       = 0;
  $total['cramount'] = 0;
  $total['crfine']   = 0;
  $total['dramount'] = 0;
  $total['totalAmt'] = 0;
  $netfine           = 0;
  $netamount         = 0;
  $b                 = 0;
  
	$bullionPartyName   = '';
	$bullionAddress   = '';
	$partyMobile   = '';
	$bullionFromDate   = '';
	
	$selectQueryParty = "SELECT partyId,bullionPartyName,bullionAddress,partyMobile,bullionFromDate 
	                       FROM bullionparty
	                      WHERE partyId = ".$_REQUEST['partyId'];
	$selectQueryPartyResult = mysql_query($selectQueryParty);
	$selectQueryPartyNumRow = mysql_num_rows($selectQueryPartyResult);
	if($selectQueryPartyNumRow > 0)
	{
		while($bullionPartyNamepartyRow = mysql_fetch_array($selectQueryPartyResult))
		{
			$partyId          = $bullionPartyNamepartyRow['partyId'];
			$bullionPartyName = $bullionPartyNamepartyRow['bullionPartyName'];
			$bullionAddress   = $bullionPartyNamepartyRow['bullionAddress'];
			$partyMobile      = $bullionPartyNamepartyRow['partyMobile'];
		}
	 }
	
if(isset($_POST['party']) && $_POST['party'] > 0)
{ 
    $partyId   = $_POST['party'];
    $toDate    = $_POST['toDateYear']."-".$_POST['toDateMonth']."-".$_POST['toDateDay'];
    $selectbullionDetail = "SELECT bulliondetail.bulliondetailid,bullionmaster.bullionmasterid,bullionmaster.partyId,bulliondetail.bullionmasterid,DATE_FORMAT(bullionmaster.bulliondate,'%Y-%m-%d') AS bulliondate,bulliondetail.particulars,bulliondetail.kg,bulliondetail.bhav,bulliondetail.cramount,bulliondetail.crfine,bulliondetail.dramount
                               FROM  bulliondetail
                               JOIN  bullionmaster ON bulliondetail.bullionmasterid = bullionmaster.bullionmasterid
                              WHERE  bullionmaster.partyId = ".$_POST['party']."
                                AND  bullionmaster.bulliondate <= '".$toDate."'
                              ORDER BY bullionmaster.bulliondate, bullionmaster.bulliondate";
    $selectbullionDetailRes = mysql_query($selectbullionDetail);
    $bullionDetailRow = mysql_num_rows($selectbullionDetailRes);
     $total['netfine']   = 0;
     $total['netamount'] = 0;
    while($bullionDetailRow = mysql_fetch_array($selectbullionDetailRes))
    {
      $bullionDetail[$b]['bulliondetailid'] = $bullionDetailRow['bulliondetailid'];
      $bullionDetail[$b]['bullionmasterid'] = $bullionDetailRow['bullionmasterid'];
      $bullionDetail[$b]['bulliondate']     = $bullionDetailRow['bulliondate'];
      $bullionDetail[$b]['particulars']     = $bullionDetailRow['particulars'];
      $bullionDetail[$b]['kg']              = $bullionDetailRow['kg'];
      $bullionDetail[$b]['bhav']            = $bullionDetailRow['bhav'];
      $bullionDetail[$b]['cramount']        = $bullionDetailRow['cramount'];
      $bullionDetail[$b]['crfine']          = $bullionDetailRow['crfine'];
      $bullionDetail[$b]['dramount']        = $bullionDetailRow['dramount'];
      $b++;
      
      $total['kg']       += $bullionDetailRow['kg'];
      $total['cramount'] += $bullionDetailRow['cramount'];
      $total['crfine']   += $bullionDetailRow['crfine'];
      $total['dramount'] += $bullionDetailRow['dramount'];
      $total['totalAmt'] += $bullionDetailRow['dramount'] - $bullionDetailRow['cramount'];
    }
     $total['netfine']   = ($total['kg'] - $total['crfine']);
     $total['netamount'] = ($total['dramount'] - $total['cramount']);
  }
	// Selection OF Bullion Prev Fine ANd Amount :End
	 
	if(isset($_POST['delete']))
	{
		$bullionFromDate     = isset($_REQUEST['toDateYear']) ? $_REQUEST['toDateYear']."-".$_REQUEST['toDateMonth']."-".$_REQUEST['toDateDay'] : 0;
	  $partyId   = isset($_POST['party']) ? $_POST['party'] : 0;
	  //Delete Records Before INSERT Opening :Start
    $newOpeningDate       = date("Y-m-d",mktime(0,0,0,substr($toDate,5,2),substr($toDate,8,2)+1,substr($toDate,0,4)));
    $deleteQueryDetailBullion =  "DELETE FROM bulliondetail
                                   WHERE bullionmasterid IN 
                                         (SELECT bullionmasterid FROM bullionmaster WHERE partyId = ".$partyId.")";
    $deleteQueryDetailBullionRes = mysql_query($deleteQueryDetailBullion);
    // Master Delete :Start
    $deleteQueryBullion   = "DELETE FROM  bullionmaster
                              WHERE partyId = ".$partyId."
                                AND bulliondate <= '".$bullionFromDate."'";
    $deleteQueryBullionRes  = mysql_query($deleteQueryBullion);
    // Master Delete :Start
    //Delete Records Before INSERT Opening :Start
  }
		
  $smarty = new Smarty;
  require('bottom.php');
  $smarty->assign("l_editParty",$l_editParty);
  $smarty->assign("l_bullionPartyName",$l_bullionPartyName);

  $smarty->assign("partyId",$partyId);
  $smarty->assign("bullionPartyName",$bullionPartyName);
  $smarty->assign("bullionAddress",$bullionAddress);
  $smarty->assign("partyMobile",$partyMobile);
  $smarty->assign("bullionFromDate",$bullionFromDate);
  $smarty->assign("total",$total);

	$smarty->display('bullionPartyEdit.tpl');
?>
